import { getTenantPage, removeTenant, TenantVO } from '@/services/TenantService'
import { useModel } from '@@/exports'
import { ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Popconfirm, Space } from 'antd'

export interface TenantProps {
  onSave?: () => void
  onUpdate?: (record: TenantVO) => void
  tableRef?: any
}

const Tenant = (props: TenantProps) => {
  let { getDictEnum, getDictData } = useModel('dict')

  /**
   * 列定义
   */
  const columns: ProColumns<TenantVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      width: 120,
      hideInTable: true,
      hideInSearch: true,
    },
    {
      title: '租户名',
      dataIndex: 'tenantName',
      width: 350,
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 120,
      valueEnum: getDictEnum('tenant_status'),
      hideInSearch: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '操作',
      render: (_: any, record: TenantVO) => [
        <Space key="operate">
          <Button type="link" size="small" onClick={() => props.onUpdate && props.onUpdate(record)}>
            编辑
          </Button>
          <Popconfirm
            title="确定要删除吗"
            onConfirm={() => {
              removeTenant(record.id).then(() => props.tableRef?.current?.resetFields())
            }}
            okText="确认"
            cancelText="取消"
          >
            <Button type="link" danger size="small">
              删除
            </Button>
          </Popconfirm>
        </Space>,
      ],
      fixed: 'right',
      width: 150,
      hideInSearch: true,
    },
  ]

  // 工具栏
  let actions = [
    props?.onSave && (
      <Button key="create" type="primary" onClick={props.onSave}>
        添加
      </Button>
    ),
  ]

  return (
    <>
      <ProTable
        actionRef={props.tableRef}
        columns={columns}
        request={getTenantPage}
        toolbar={{
          actions: actions,
        }}
        scroll={{ x: 500 }}
        sticky={{ offsetHeader: 55, offsetScroll: 5 }}
      ></ProTable>
    </>
  )
}

export default Tenant
